package net.toby200.canalbuddy.notices.text;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.toby200.canalbuddy.R;
import net.toby200.canalbuddy.notices.model.Notice;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by toby on 01/07/2014.
 */
public class NoticeDetailFragment extends Fragment {
    private static final Logger LOG = Logger.getLogger(NoticeDetailFragment.class.getName());

    Notice notice;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        Bundle noticeBundle = getArguments();
        if (noticeBundle == null) {
            noticeBundle = bundle;
        }

        if (noticeBundle != null) {
            try {
                this.notice = noticeBundle.getParcelable("notice");
            } catch (Exception e) {
                LOG.log(Level.SEVERE, "Unable to extract notice from bundle", e);
            }
        } else {
            LOG.warning("No bundle in noticeDetailFragment.onCreate()");
        }
    }

    @Override
    public void onSaveInstanceState(Bundle saveState) {
        super.onSaveInstanceState(saveState);
        saveState.putParcelable("notice", notice);
    }


    @Override
    public void onStart() {
        super.onStart();

        if (notice != null) {
            TextView title = (TextView) getActivity().findViewById(R.id.detailTitle);
            title.setText(notice.getTitle());

            TextView waterway = (TextView) getActivity().findViewById(R.id.waterway);
            waterway.setText(notice.getWaterway());

            TextView locationDetail = (TextView) getActivity().findViewById(R.id.location_detail);
            locationDetail.setText(notice.getLocationDetail());

            TextView fromDate = (TextView) getActivity().findViewById(R.id.detailFromDate);
            fromDate.setText(notice.getFromDate());

            TextView toDate = (TextView) getActivity().findViewById(R.id.detailToDate);
            toDate.setText(notice.getToDate());

            TextView descTextView = (TextView) getActivity().findViewById(R.id.detailDesc);
            descTextView.setText(notice.getDescription());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.notice_detail_view, container, false);
    }

}
